class Solution {
    public:
        long long continuousSubarrays(vector<int>& nums) {
            multiset<int> mst;
            int n = nums.size();
            long long ans = 0;
            int i = 0;
            int j = 0;
            while (j < n) {
                mst.insert(nums[j]);
                int min = *mst.begin();
                int max = *mst.rbegin();
                while (max - min > 2) {
                    multiset<int>::iterator it = mst.find(nums[i]);
                    mst.erase(it);
                    i++;
                    min = *mst.begin();
                    max = *mst.rbegin();
                }
                ans += j - i + 1;
                j++;
            }
            return ans;
        }
    };